package com.wingy.psyduckplayer.model;


import android.content.Context;

import com.wingy.psyduckplayer.R;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class PropertyBean {
    public static String[] THEMES;
    private static String DEFAULT_THEME;
    public static String[] PLAYMODE;
    private static String DEFAULT_MODE;
    //应用上下文
    private Context context;

    public void setAndSaveTheme(String theme) {
        this.theme = theme;
        this.saveTheme(theme);
    }

    public String getTheme() {
        return theme;
    }

    //主题
    private String theme;

    public String getPlaymode() {
        return playmode;
    }

    public void setAndSavePlaymode(String playmode) {
        this.playmode = playmode;
        this.savePLayMode(playmode);
    }

    //播放模式
    private String playmode;


    public PropertyBean(Context context) {
        this.context = context;
        THEMES = context.getResources().getStringArray(R.array.theme);
        DEFAULT_THEME = THEMES[0];
        PLAYMODE = context.getResources().getStringArray(R.array.playmode);
        DEFAULT_MODE = PLAYMODE[0];
        this.loadTheme();
        this.loadMode();
    }

    /**读取播放模式*/
    private void loadMode() {
        Properties properties = new Properties();
        try {
            FileInputStream stream = context.openFileInput("configurationplaymode.cfg");
            properties.load(stream);
            playmode = properties.getProperty("playmode").toString();
        }catch (Exception e) {
            this.savePLayMode(DEFAULT_THEME);
        }
    }

    /**保存播放模式*/
    private boolean savePLayMode(String playmode) {
        Properties properties = new Properties();
        properties.put("playmode", playmode);
        try {
            FileOutputStream stream = context.openFileOutput("configurationplaymode.cfg",context.MODE_PRIVATE);
            properties.store(stream, " ");
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**读取主题*/
    private void loadTheme() {
        Properties properties = new Properties();
        try {
            FileInputStream stream = context.openFileInput("configuration.cfg");
            properties.load(stream);
            theme = properties.getProperty("theme").toString();
        }catch (Exception e) {
            this.saveTheme(DEFAULT_THEME);
        }
    }

    /**保存主题到configuration.cfg*/
    private boolean saveTheme(String theme) {
        Properties properties = new Properties();
        properties.put("theme", theme);
        try {
            FileOutputStream stream = context.openFileOutput("configuration.cfg",context.MODE_PRIVATE);
            properties.store(stream, " ");
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
